package cn.edu.zhku.citrusmonitor.baseinfo.pest.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import cn.edu.zhku.citrusmonitor.baseinfo.pest.service.PestService;
import cn.edu.zhku.citrusmonitor.baseinfo.pest.service.PestServiceImpl;
import cn.edu.zhku.citrusmonitor.constant.Constant;
import cn.edu.zhku.citrusmonitor.domain.Bean;
import cn.edu.zhku.citrusmonitor.domain.TbPest;
import cn.edu.zhku.citrusmonitor.util.BaseServlet;
import cn.edu.zhku.citrusmonitor.util.FileOperationUtil;
import cn.edu.zhku.citrusmonitor.util.Message;
import cn.edu.zhku.citrusmonitor.util.xy.PageBean;
import net.sf.json.JSONObject;

/**
 * @author 欣裕
 * @date 2018年8月2日
 * 
 */
@WebServlet("/pest.do")
public class PestServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * 修改虫害信息，后台管理
	 * @param request
	 * @param response
	 * @return PS.modifyPest(pest)
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public JSONObject modify(HttpServletRequest request,HttpServletResponse response) throws IllegalAccessException, InvocationTargetException {
		TbPest pest = new TbPest();
		PestService PS = new PestServiceImpl();
		BeanUtils.populate(pest, request.getParameterMap());
		return PS.modifyPest(pest);
	}
	/**
	 * 删除某条虫害信息,后台管理
	 * @param request
	 * @param response
	 * @return PS.removePest(pest)
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public JSONObject remove(HttpServletRequest request,HttpServletResponse response) throws IllegalAccessException, InvocationTargetException {
		TbPest pest = new TbPest();
		PestService PS = new PestServiceImpl();
		BeanUtils.populate(pest, request.getParameterMap());
		return PS.removePest(pest);
	}
	/**
	 * 添加虫害信息，后台管理
	 * @param request
	 * @param response
	 * @return PS.addPest(pest)
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public JSONObject add(HttpServletRequest request,HttpServletResponse response) throws IllegalAccessException, InvocationTargetException {
		TbPest pest = new TbPest();
		
		// 处理上传文件表单
		try {
			Map<String, String[]> map = FileOperationUtil.upload(request, Constant.PEST_IMG_PATH);
			BeanUtils.populate(pest, map);
			PestService PS = new PestServiceImpl();
			return PS.addPest(pest);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return JSONObject.fromObject(new Message("2","添加失败"));	
		
		
	}
	/**
	 * 根据id显示虫害正文，后台管理
	 * @param request
	 * @param response
	 * @return JSON.toJSONStringWithDateFormat(PS.getPest(id), "yyyy-MM-dd", SerializerFeature.EMPTY)
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	public String findPestByPestId(HttpServletRequest request,HttpServletResponse response) throws IllegalAccessException, InvocationTargetException {
		PestService PS = new PestServiceImpl();
		String id = request.getParameter("pestId");
		return JSON.toJSONStringWithDateFormat(PS.getPest(id), "yyyy-MM-dd", SerializerFeature.EMPTY);
	}
	
	/**
	 * 根据id显示虫害正文，前端显示
	 * @param request
	 * @param response
	 * @return JSON.toJSONStringWithDateFormat(PS.getPest(id), "yyyy-MM-dd", SerializerFeature.EMPTY)
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	public String findPestById(HttpServletRequest request,HttpServletResponse response) throws IllegalAccessException, InvocationTargetException {
		PestService PS = new PestServiceImpl();
		String id = request.getParameter("id");
		return JSON.toJSONStringWithDateFormat(PS.showPest(id), "yyyy-MM-dd", SerializerFeature.EMPTY);
	}
	
	/**
	 * 分写显示全部虫害列表(前端显示)
	 * @param request
	 * @param response
	 * @return JSON.toJSONStringWithDateFormat(pbn, "yyyy-MM-dd", SerializerFeature.EMPTY) 规定pageSize（默认10条）的一页虫害列表
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public String findAll(HttpServletRequest request,HttpServletResponse response) throws IllegalAccessException, InvocationTargetException {
		PestService PS = new PestServiceImpl();
		Bean bean = new Bean();
		PageBean<Bean> pbn = new PageBean<Bean>();
		BeanUtils.populate(bean, request.getParameterMap());
		BeanUtils.populate(pbn, request.getParameterMap());	//默认一页十条
		pbn = PS.findPestPageBean(pbn.getCurrentPage(), pbn.getPageSize());
		return JSON.toJSONStringWithDateFormat(pbn, "yyyy-MM-dd", SerializerFeature.EMPTY);
	}
	
	/**
	 * 最近的虫害列表，前端显示
	 * @param request
	 * @param response
	 * @return JSON.toJSONStringWithDateFormat(PS.findRecentPest(pageSize, pest), "yyyy-MM-dd", SerializerFeature.EMPTY)
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	public String recent(HttpServletRequest request,HttpServletResponse response) throws IllegalAccessException, InvocationTargetException {
		int pageSize = 0;
		PestService PS = new PestServiceImpl();
		Bean bean = new Bean();
		PageBean<Bean> pbn = new PageBean<Bean>();
		BeanUtils.populate(pbn, request.getParameterMap());	//默认一页十条
		pageSize = pbn.getPageSize();
		return JSON.toJSONStringWithDateFormat(PS.findRecentPest(pageSize, bean), "yyyy-MM-dd", SerializerFeature.EMPTY);
	}
	
	
	/**
	 * 分写显示全部虫害列表(后台显示，更多信息)
	 * @param request
	 * @param response
	 * @return JSON.toJSONStringWithDateFormat(pbn, "yyyy-MM-dd", SerializerFeature.EMPTY) 规定pageSize（默认10条）的一页虫害列表
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public String findAllPestDetail(HttpServletRequest request,HttpServletResponse response) throws IllegalAccessException, InvocationTargetException {
		PestService PS = new PestServiceImpl();
		TbPest pest = new TbPest();
		PageBean<TbPest> pbn = new PageBean<TbPest>();
		BeanUtils.populate(pest, request.getParameterMap());
		BeanUtils.populate(pbn, request.getParameterMap());	//默认一页十条
		pbn = PS.findAllPestDetail(pbn.getCurrentPage(), pbn.getPageSize());
		return JSON.toJSONStringWithDateFormat(pbn, "yyyy-MM-dd", SerializerFeature.EMPTY);
	}
	
	/**
	 * 搜索查询，分页显示，后台管理
	 * @param request
	 * @param response
	 * @return 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	public String searchPest(HttpServletRequest request,HttpServletResponse response) throws IllegalAccessException, InvocationTargetException {
		PestService PS = new PestServiceImpl();
		TbPest pest = new TbPest();
		PageBean<TbPest> pbn = new PageBean<TbPest>();
		BeanUtils.populate(pest, request.getParameterMap());
		BeanUtils.populate(pbn, request.getParameterMap());	//默认一页十条
		String word = request.getParameter("word");
		Integer pageSize = pbn.getPageSize();
		Integer currentPage = pbn.getCurrentPage();
		pbn = PS.findAllPestDetailByWord(currentPage, pageSize, word);
		return JSON.toJSONStringWithDateFormat(pbn, "yyyy-MM-dd", SerializerFeature.EMPTY);
	}
	
	/**
	 * 批量删除虫害
	 * @param request
	 * @param response
	 * @return 
	 */
	public JSONObject deleteInBatch(HttpServletRequest request,HttpServletResponse response) {
		// 1.得到所有要删除的虫害id
		String param = request.getParameter("ids");
		String[] ids = param.split(",");
		// 2.调用service，完成批量删除
		PestService PS = new PestServiceImpl(); 
		return PS.delSelect(ids);
	}
}
